package com.frxuexi.mdm_2207.demo

import android.util.Log
import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.LazyRow
import androidx.compose.foundation.lazy.grid.GridCells
import androidx.compose.foundation.lazy.grid.LazyHorizontalGrid
import androidx.compose.foundation.lazy.grid.LazyVerticalGrid
import androidx.compose.foundation.lazy.grid.items
import androidx.compose.foundation.lazy.items
import androidx.compose.foundation.lazy.staggeredgrid.LazyVerticalStaggeredGrid
import androidx.compose.foundation.lazy.staggeredgrid.StaggeredGridCells
import androidx.compose.foundation.lazy.staggeredgrid.items
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Build
import androidx.compose.material.icons.filled.Favorite
import androidx.compose.material.icons.filled.Send
import androidx.compose.material3.AlertDialog
import androidx.compose.material3.Button
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.Icon
import androidx.compose.material3.Text
import androidx.compose.material3.TextButton
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import coil3.compose.AsyncImage
import com.frxuexi.mdm_2207.R
import java.text.SimpleDateFormat

data class NewsEntity(val img:String, val title:String, val time:String, var count:Int)
@Preview
@Composable
fun Demo03() {
    var checkedName by remember {
        mutableStateOf("")
    }
    val list = mutableListOf<NewsEntity>()
    for (i in 1..20){
        val time = SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(System.currentTimeMillis())
        list.add(NewsEntity("https://img0.baidu.com/it/u=3710358681,2408493409&fm=253&app=120&size=w931&n=0&f=JPEG&fmt=auto?sec=1734022800&t=3d8ab736865c1560039d4e114ff2e5fd","王五"+i,time,0))
    }
    LazyColumn(Modifier.padding(10.dp),verticalArrangement = Arrangement.spacedBy(10.dp)){
        items(list){
            NewsItem(entity = it,it.title==checkedName,){
                checkedName=it.title
            }
        }
    }
    /*LazyRow(Modifier.padding(10.dp), horizontalArrangement = Arrangement.spacedBy(10.dp)){
        items(list){
            NewsItem(entity = it)
        }
    }*/

    /*LazyVerticalGrid(columns = GridCells.Fixed(2), content = {
        items(list){
            NewsItem(entity = it)
        }
    })*/
    /*LazyHorizontalGrid(rows = GridCells.Fixed(2), content = {
        items(list){
            NewsItem(entity = it)
        }
    })*/

    /*LazyVerticalStaggeredGrid(columns = StaggeredGridCells.Fixed(2), content = {
        items(list){
            NewsItem(entity = it)
        }
    })*/
}

@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun NewsItem(entity: NewsEntity,ischeck:Boolean,onClick: () -> Unit){
    var res by remember {
        mutableStateOf(false)
    }
    var isShow by remember {
        mutableStateOf(false)
    }

    /*var checked by remember {
        mutableStateOf(R.drawable.ic_action_uncheck)
    }*/

    Row(Modifier.clickable {
        isShow=true
    }, verticalAlignment = Alignment.CenterVertically) {
        var ima = if(ischeck)  R.drawable.ic_action_check else  R.drawable.ic_action_uncheck
        Image(painter = painterResource(id = ima /*checked*/), contentDescription = "",Modifier.clickable {
            /*if(checked==R.drawable.ic_action_uncheck) {
                checked = R.drawable.ic_action_check
            }else{
                checked=R.drawable.ic_action_uncheck
            }*/
            onClick()
        })

        AsyncImage(model = entity.img, contentDescription = "", modifier = Modifier
            .size(100.dp)
            .clip(
                CircleShape
            ), contentScale = ContentScale.Crop)
        Spacer(modifier = Modifier.width(10.dp))
        Column(Modifier.height(100.dp), verticalArrangement = Arrangement.SpaceAround) {
            Text(text = entity.title)
            Text(text = entity.time)
            Row {
                var image = if(res)  R.drawable.ic_action_zan else  R.drawable.ic_action_nozan
                Image(painter = painterResource(id = image), contentDescription = "",
                    Modifier.clickable {
                        res=!res
                        if(res){
                            entity.count+=1
                        }else{
                            entity.count-=1
                        }
                    })
                Spacer(modifier = Modifier.width(5.dp))
                Text(text = entity.count.toString())
            }
        }
    }

    if(isShow){
        /*AlertDialog(onDismissRequest = { isShow=false }, confirmButton = {
            Button(onClick = {
                Log.i("===","确认点击了")
                isShow=false
            }) {
                Text(text = "确认")
            }
        },dismissButton={
            Button(onClick = { isShow=false }) {
                Text(text = "取消")
            }
        }, icon = { Icon(imageVector = Icons.Default.Build, contentDescription = "")},
           title = { Text(text = "信息提示")},
            text = { Text(text = "这是${entity.title}的具体信息")})*/
        AlertDialog(onDismissRequest = { isShow=false }) {
            Column(
                Modifier
                    .fillMaxWidth()
                    .background(Color.White, RoundedCornerShape(20.dp))
                    .padding(20.dp)) {
                AsyncImage(model = entity.img, contentDescription = "", modifier = Modifier
                    .size(100.dp)
                    .clip(
                        CircleShape
                    ), contentScale = ContentScale.Crop)
                Text(text = entity.title)

                Row(Modifier.fillMaxWidth(), horizontalArrangement = Arrangement.SpaceAround) {
                    Button(onClick = { isShow=false }) {
                        Row {
                            Icon(imageVector = Icons.Default.Send, contentDescription = "")
                            Text(text = "确认")
                        }

                    }
                    TextButton(onClick = { isShow=false }) {
                        Text(text = "取消")
                    }
                }
            }
        }

    }
}